//
//  EHTabBarController.swift
//  ExtremeHealth
//
//  Created by Zhongkai Li 李忠凯 on 2018/9/26.
//  Copyright © 2018 GanDongKeJi. All rights reserved.
//

import ESTabBarController_swift
import UIColor_Hex_Swift

class EHTabBarController: ESTabBarController {
    init() {
        super.init(nibName: nil, bundle: nil)
        shouldHijackHandler = { $2 == 1 }
        didHijackHandler = { tabBarController, viewController, index in
            print("ok")
        }

        let homePage = generateHomePage()
        let plusIcon = UIViewController()
        let accountPage = UIViewController()

        homePage.tabBarItem = ESTabBarItem.init(ExampleBasicContentView(),
                                                title: "首页",
                                                image: UIImage(named: "icon_home_unselected"),
                                                selectedImage: UIImage(named: "icon_home_selected"))
        plusIcon.tabBarItem = ESTabBarItem.init(ExampleIrregularityContentView(),
                                                title: nil,
                                                image: UIImage(named: "plus"),
                                                selectedImage: UIImage(named: "plus"))
        accountPage.tabBarItem = ESTabBarItem.init(ExampleBasicContentView(),
                                                   title: "我的",
                                                   image: UIImage(named: "icon_my_unselected"),
                                                   selectedImage: UIImage(named: "icon_my_selected"))
        viewControllers = [homePage, plusIcon, accountPage]
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        tabBar.isTranslucent = false
        tabBar.tintColor = .red
    }

    private func generateHomePage() -> UIViewController {
        let storyboard = UIStoryboard.fromName("Home")
        let controller = storyboard.instantiateViewController(withIdentifier: "home")
        let navi = UINavigationController(rootViewController: controller)
        navi.setNavigationBarHidden(true, animated: false)
        return navi
    }
}

class ExampleIrregularityContentView: ESTabBarItemContentView {

    override init(frame: CGRect) {
        super.init(frame: frame)

        self.imageView.backgroundColor = UIColor("#00CFCC")
        self.imageView.layer.cornerRadius = 30
        self.insets = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
        let transform = CGAffineTransform.identity
        self.imageView.transform = transform

        iconColor = UIColor.init(white: 255.0 / 255.0, alpha: 1.0)
        highlightIconColor = UIColor.init(white: 255.0 / 255.0, alpha: 1.0)
        backdropColor = .clear
        highlightBackdropColor = .clear
    }

    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func updateLayout() {
        super.updateLayout()
        self.imageView.sizeToFit()
        self.imageView.center = CGPoint.init(x: self.bounds.size.width / 2.0, y: self.bounds.size.height / 2.0)
    }
}

class ExampleBasicContentView: ESTabBarItemContentView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        textColor = .black
        highlightTextColor = .black
        iconColor = .black
        highlightIconColor = .black
    }

    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
